﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageDevicetable.Queries
{
    public class GetDeviceModelByIdQueries : IRequest<Json<GetDeviceModelByIdDTO>>
    {
        public int id { get; set; }
    }

    public class GetDeviceModelByIdQueriesHandler : IRequestHandler<GetDeviceModelByIdQueries, Json<GetDeviceModelByIdDTO>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IDevicetableRepository _idevicetableRepository;
        public GetDeviceModelByIdQueriesHandler(ICurrentUserService icurrentUserServices, IDevicetableRepository idevicetableRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _idevicetableRepository = idevicetableRepository;
        }

        public async Task<Json<GetDeviceModelByIdDTO>> Handle(GetDeviceModelByIdQueries request, CancellationToken cancellationToken)
        {
            GetDeviceModelByIdDTO model = new GetDeviceModelByIdDTO() { };
            if (request.id <= 0)
            {
                return Json<GetDeviceModelByIdDTO>.Failed("请选择需要展示的设备信息", model);
            }
            else
            {
                int userid = _icurrentUserServices.GetUserId();
                model = await _idevicetableRepository.GetDeviceModelById(userid, request.id, cancellationToken);
                if (model != null && model.id > 0)
                {
                    return Json<GetDeviceModelByIdDTO>.Success("查询成功", model);
                }
                else {
                    return Json<GetDeviceModelByIdDTO>.Failed("请选择有效的设备", model);
                }
            }

        }
    }
}
